package com.mes.schedule.dao;

import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.mes.common.dao.BasicDao;
import com.mes.common.exception.db.BaseDBException;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.mes.schedule.dao.iface.IResourceSegementDao;
import com.mes.schedule.dao.iface.ISOperationLinkDao;
import com.mes.schedule.domain.ResourceSegement;
import com.mes.schedule.domain.SOperationLink;

@Repository
public class SOperationLinkDao extends BasicDao<SOperationLink> implements ISOperationLinkDao{
	private static final Log log = LogFactory.getLog(SOperationLinkDao.class);
	
	/**
	 * @see the method use for ScheduelManager
	 * @param scheduleUid
	 * @throws BaseDBException
	 */
	public void deleteAllLink(String scheduleUid) throws BaseDBException {
		try {

			Session session = this.getSession();
			Query query=session.createQuery("delete SOperationLink t where t.scheduleUid=?");
			query.setString(0, scheduleUid);
			query.executeUpdate();

		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ;
	}
	
	/**
	 * @see the method use for STaskDao
	 * @param scheduleUid
	 * @return
	 */
	public List<SOperationLink> getScheduleOperationLinkList(String scheduleUid){
		
		List<SOperationLink> sOperationLinkList = new ArrayList<SOperationLink>();
		Session session = this.getSession();
		Criteria crit = session.createCriteria(SOperationLink.class); 
		crit.add(Restrictions.eq("scheduleUid", scheduleUid));
		sOperationLinkList = crit.list();
		
		return sOperationLinkList;
	}

}
